package com.hl.apilibrary;

import android.content.Context;

import com.hl.apilibrary.bean.EmotionGroup;
import com.hl.apilibrary.bean.Sticky;
import com.hl.apilibrary.bean.TTF;
import com.hl.apilibrary.bean.TextBubble;
import com.hl.apilibrary.bean.Theme;

import java.util.HashMap;
import java.util.List;

import okhttp3.ResponseBody;

public class ApiHelper {
    private static ApiHelper helper = null;

    private ApiHelper() {

    }

    public static ApiHelper getInstance() {
        if (helper == null) {
            synchronized (ApiHelper.class) {
                if (helper == null) {
                    helper = new ApiHelper();
                }
            }
        }
        return helper;
    }

    public void download(Context context, String url, final String path, final OnDownLoadCallback callback) {
        if (callback != null)
            callback.onStart();
        new RetrofitHelper<ResponseBody>().enqueue(Api.getApiService(context).download(url), new ApiCallback<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody result) {
                new Thread(new DownLoadRunnable(result, path, callback)).start();
            }

            @Override
            public void onFailure(String msg) {
                if (callback != null) {
                    callback.onError(msg);
                }
            }
        });
    }

    /**
     * 获取字体列表
     *
     * @param context
     * @param callback
     */
    public void getTTF(Context context, final OnResultCallback<List<TTF>> callback) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("lang_type", 0);
        new ApiUtil<List<TTF>>().post(Api.getApiService(context).getTTF(params), callback);
    }

    /**
     * 获取文本框
     * @param context
     * @param callback
     */
    public void getTextBubble(Context context, final OnResultCallback<List<TextBubble>> callback) {
        HashMap<String, Object> params = new HashMap<>();
//        params.put("type", 0);//气泡类型（0标签气泡，1图文气泡）
//        params.put("page", 0);//分页(页数，如第几页。)
        new ApiUtil<List<TextBubble>>().post(Api.getApiService(context).getTextBubble(params), callback);
    }

    /**
     * 获取主题列表
     *
     * @param context
     * @param callback
     */
    public void getTheme(Context context, final OnResultCallback<List<Theme>> callback) {
        HashMap<String, Object> params = new HashMap<>();
//        params.put("page", 0);
        new ApiUtil<List<Theme>>().post(Api.getApiService(context).getTheme(params), callback);
    }

    /**
     * 获取某个主题样式
     *
     * @param context
     * @param id
     * @param callback
     */
    public void getThemeById(Context context, int id, final OnResultCallback<Theme> callback) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", id);
        new ApiUtil<Theme>().post(Api.getApiService(context).getThemeById(params), callback);
    }

    public void getSticky(Context context, final OnResultCallback<List<Sticky>> callback) {
        HashMap<String, Object> params = new HashMap<>();
        new ApiUtil<List<Sticky>>().post(Api.getApiService(context).getSticky(params), callback);
    }

    public void getStickyById(Context context, int id, final OnResultCallback<Sticky> callback) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", id);
        new ApiUtil<Sticky>().post(Api.getApiService(context).getStickyById(params), callback);
    }

    public void getEmotion(Context context, final OnResultCallback<List<EmotionGroup>> callback) {
        HashMap<String, Object> params = new HashMap<>();
        new ApiUtil<List<EmotionGroup>>().post(Api.getApiService(context).getEmotion(params), callback);
    }
}
